package com.gubei.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.gubei.subject.common.enums.CategoryTypeEnum;
import com.gubei.subject.common.enums.IsDeletedFlagEnum;
import com.gubei.subject.domain.convert.SubjectLabelConverter;
import com.gubei.subject.domain.entity.SubjectLabelBO;
import com.gubei.subject.domain.service.SubjectLabelDomainService;
import com.gubei.subject.infra.basic.entity.SubjectCategory;
import com.gubei.subject.infra.basic.entity.SubjectLabel;
import com.gubei.subject.infra.basic.entity.SubjectMapping;
import com.gubei.subject.infra.basic.service.SubjectCategoryService;
import com.gubei.subject.infra.basic.service.SubjectLabelService;
import com.gubei.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    @Resource
    SubjectLabelService subjectLabelService;

    @Resource
    SubjectCategoryService subjectCategoryService;

    @Resource
    SubjectMappingService subjectMappingService;

    @Override
    public Boolean add(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.add.SubjectLabelBO:{}", JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertLabelBOToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        int count = subjectLabelService.insert(subjectLabel);
        return count > 0;
    }

    @Override
    public Boolean update(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.update.SubjectLabelBO:{}", subjectLabelBO);
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertLabelBOToLabel(subjectLabelBO);
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    @Override
    public Boolean delete(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
           log.info("SubjectLabelDomainServiceImpl.delete.SubjectLabelBO:{}", subjectLabelBO);
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertLabelBOToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {

        if(log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.queryLabelByCategoryId:{}", subjectLabelBO);
        }
        SubjectCategory subjectCategory = subjectCategoryService.queryById(subjectLabelBO.getCategoryId());
        //現在の分類が大分類である場合、すべてのラベルをクエリ
        if(CategoryTypeEnum.PRIMARY.getCode() == subjectCategory.getCategoryType()) {
            SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertLabelBOToLabel(subjectLabelBO);
            List<SubjectLabel> subjectLabelList = subjectLabelService.queryByCondition(subjectLabel);
            List<SubjectLabelBO> subjectLabelBOList = SubjectLabelConverter.INSTANCE.convertLabelListToLabelBOList(subjectLabelList);
            return subjectLabelBOList;
        }
        //現在の分類が二級分類である場合、分類idに基づいて、ラベルをクエリ
        Long categoryId = subjectLabelBO.getCategoryId();
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(categoryId);
        subjectMapping.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        List<SubjectMapping> subjectMappingList = subjectMappingService.queryLabelId(subjectMapping);
        if(CollectionUtils.isEmpty(subjectMappingList)){
            return Collections.emptyList();
        }
        List<Long> labelIdList = subjectMappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
        List<SubjectLabel> subjectLabelList = subjectLabelService.batchQueryById(labelIdList);
        List<SubjectLabelBO> subjectLabelBOList = new LinkedList<>();
        subjectLabelList.forEach(subjectLabel -> {
            SubjectLabelBO labelBO = new SubjectLabelBO();
            labelBO.setId(subjectLabel.getId());
            labelBO.setCategoryId(categoryId);
            labelBO.setLabelName(subjectLabel.getLabelName());
            labelBO.setSortNum(subjectLabel.getSortNum());
            subjectLabelBOList.add(labelBO);
        });

        //Stream流
        List<SubjectLabelBO> boList = subjectLabelList.stream().map(subjectLabel -> {
            SubjectLabelBO labelBO = new SubjectLabelBO();
            labelBO.setId(subjectLabel.getId());
            labelBO.setCategoryId(categoryId);
            labelBO.setLabelName(subjectLabel.getLabelName());
            labelBO.setSortNum(subjectLabel.getSortNum());
            return labelBO;
        }).collect(Collectors.toList());
        return boList;
    }
}
